package org.springframework.prospring.ticket.dao.hibernate;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.prospring.ticket.dao.FieldDAO;
import org.springframework.prospring.ticket.dao.RegionDAO;
import org.springframework.prospring.ticket.domain.Turn;
import org.springframework.prospring.ticket.domain.Field;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.exception.BusinessException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.StringUtils;

import java.util.Set;
import java.util.Collection;

/**
 * This class interacts with Spring and Hibernate to save and
 * retrieve User objects.
 *
 * @author Alexis Quiros
 */
public class RegionDAOHibernate extends HibernateDaoSupport implements RegionDAO {
    private static Log log = LogFactory.getLog(RegionDAOHibernate.class);

    public RegionDAOHibernate() {
        super();
        if (RegionDAOHibernate.log.isDebugEnabled()) {
            RegionDAOHibernate.log.debug("in RegionDAOHibernate Constructor");
        }
    }

    public Collection<Region> findAllRegions(){
        if (RegionDAOHibernate.log.isDebugEnabled()) {
            RegionDAOHibernate.log.debug("finding all Regions ");
        }

        Collection regions = getHibernateTemplate().find("from Region");

        return regions;
    }
    public void saveRegion(Region region) throws Exception {
        if (RegionDAOHibernate.log.isDebugEnabled()) {
            RegionDAOHibernate.log.debug("Saving Region: " + region);
        }

        if (StringUtils.isEmpty(region.getName())) {
            String message = "The name is required, name="+region.getName();
            RegionDAOHibernate.log.info(message);
            throw new BusinessException(message);
        }

        getHibernateTemplate().saveOrUpdate(region);

        if (RegionDAOHibernate.log.isDebugEnabled()) {
            RegionDAOHibernate.log.debug("regionId set to: " + region.getId());
        }
    }

    public void removeRegion(Long regionId) {
        if (RegionDAOHibernate.log.isDebugEnabled()) {
            RegionDAOHibernate.log.debug("removing Region id: " + regionId);
        }

        Object region = getHibernateTemplate().load(Region.class, regionId);
        getHibernateTemplate().delete(region);
    }

    public Region findByBusinessPK(String regionBusinessPK) {
        if (RegionDAOHibernate.log.isDebugEnabled()) {
            RegionDAOHibernate.log.debug("finding Region by businessPK="+regionBusinessPK);
        }

        Region region = null;
        String queryString = "from Region r where (r.businessPK = :businessPK)";
        Region regionParameter = new Region();
        regionParameter.setBusinessPK(regionBusinessPK);
        Collection<Region> regions = getHibernateTemplate().findByValueBean(queryString, regionParameter);
        //Collection<Region> regions = getHibernateTemplate().findByValueBean.find("from Region r where r.businessPK="+regionBusinessPK);

        if ((regions != null) && (!regions.isEmpty())){
            region = regions.iterator().next();
        }

        return region;
    }


    public Region findById(Long regionId) {
        if (RegionDAOHibernate.log.isDebugEnabled()) {
            RegionDAOHibernate.log.debug("finding Region by id="+regionId);
        }

        Region region = null;
        String queryString = "from Region r where (r.id = :id)";
        Region regionParameter = new Region();
        regionParameter.setId(regionId);
        Collection<Region> regions = getHibernateTemplate().findByValueBean(queryString, regionParameter);
        //Collection<Region> regions = getHibernateTemplate().findByValueBean.find("from Region r where r.businessPK="+regionBusinessPK);

        if ((regions != null) && (!regions.isEmpty())){
            region = regions.iterator().next();
        }

        return region;
    }
}
